package com.baoxin.chenxixc4coach;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.baoxin.chenxixc4coach.java.MyHandler;
import com.baoxin.chenxixc4coach.utils.PicZipUtil;

import java.util.ArrayList;
import java.util.List;

public class FeedBackActivity extends BaseActivity {

    private LinearLayout mPicLL;
    private ImageView p1;
    private ImageView p2;
    private ImageView p3;
    private ImageView p4;
    private ImageView add;

    private List<Bitmap> paths;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ImageView pic = new ImageView(this);
        ViewGroup.LayoutParams params = pic.getLayoutParams();

        handler = new MyHandler(this,activity) {
            @Override
            public void handleMessage(Message msg) {
                Bitmap bitmap = (Bitmap) msg.obj;
                //遍历前4个view
                for (int i = 0; i < 4; i++) {
                    if (i == 3) {
                        add.setVisibility(View.GONE);
                    }
                    ImageView imageView = (ImageView) mPicLL.getChildAt(i);
                    if (imageView.getVisibility() == View.GONE) {
                        imageView.setImageBitmap(bitmap);
                        imageView.setVisibility(View.VISIBLE);
                        return;
                    }
                }
            }
        };

        paths = new ArrayList<>();

        mPicLL = findViewById(R.id.pic_ll);
        p1 = findViewById(R.id.p1);
        p2 = findViewById(R.id.p2);
        p3 = findViewById(R.id.p3);
        p4 = findViewById(R.id.p4);
        add = findViewById(R.id.add);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE);

            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        //选择图片后的返回
        if (data == null)
            return;
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String localPicPath = cursor.getString(columnIndex);
        cursor.close();

        addPic(localPicPath);
    }

    private void addPic(final String path) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                Bitmap bitmap = PicZipUtil.compressImage(path);
                paths.add(bitmap);
                Message message = Message.obtain();
                message.obj = bitmap;
                handler.sendMessage(message);

            }
        };
        thread.start();
    }

    private void checkPermission(String permission) {
        if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {

            requestPermissions(new String[]{permission}, 1);

        } else {
            openAlbum();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            openAlbum();
        } else {
            showMissingPermissionDialog();
        }
    }

    /**
     * 打开本地照片库
     */
    private void openAlbum() {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, 1);
    }
}
